package com.example.gty.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.example.gty.entity.Student;
import com.example.gty.entity.StudentVo;
import com.example.gty.entity.Teacher;
import com.example.gty.mapper.StudentMapper;
import com.example.gty.service.StudentService;
import com.example.gty.service.TeacherService;
import com.example.gty.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/teacher")
@Api(tags = "老师信息管理")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentMapper studentMapper;

    @ApiOperation("查询老师所有课程及基本信息")
    @RequestMapping(value = "/selectAllTeacher", method = RequestMethod.GET)
    public ResultVo selectAllTeacher() {
        System.out.println("访问");
        return teacherService.selectAllTeacher();
    }

    @ApiOperation("关键字查询老师信息")
    @RequestMapping(value = "/selectTeacherKey", method = RequestMethod.GET)
    public ResultVo sTK(String key) {
        return teacherService.selectTeaKey(key);
    }

    @ApiOperation("修改老师基本信息")
    @RequestMapping(value = "/updateTeacher", method = RequestMethod.POST)
    public ResultVo updataTea(@RequestBody Teacher teacher) {
        return teacherService.updateTeacher(teacher);
    }

    @ApiOperation("删除老师")
    @RequestMapping(value = "/deleteTeacher", method = RequestMethod.GET)
    public ResultVo deleteTea(String tno) {
        return teacherService.deleteTeacher(tno);
    }

    @ApiOperation("添加老师信息")
    @RequestMapping(value = "/addTeacher", method = RequestMethod.POST)
    public ResultVo addTea(@RequestBody Teacher teacher, String email) {
        return teacherService.addTeacher(teacher, email);
    }

    @ApiOperation("添加老师所授课程")
    @RequestMapping(value = "/addTeacherCourse", method = RequestMethod.GET)
    public ResultVo add(String tno, String cno) {
        return teacherService.addTeacherCourse(tno, cno);
    }


    @ApiOperation("导出学生成绩信息")
    @RequestMapping(value = "/exportStudentGrade", method = RequestMethod.GET)
    public void excelStudentGrade(String sex, String cclass, String major, String college, HttpServletResponse response) throws IOException {
        List<StudentVo> list = studentMapper.selectStuMulti(sex, cclass, major, college);

        //导出工具
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("学生成绩", "sheel1"), StudentVo.class, list);
        //设置响应头
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("学生成绩.xls", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);

        outputStream.close();
        workbook.close();

    }

    @ApiOperation("导入学生基本信息")
    @RequestMapping(value = "/importStudentInfo", method = RequestMethod.GET)
    public void importStudentInfo(String filePath) {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);//字段占几列
        params.setTitleRows(1);//标题几行
        List<Student> student = ExcelImportUtil.importExcel(new File(filePath), Student.class, params);
        teacherService.importStudentInfo(student);
        for (Student student1 : student) {
            System.err.println(student1);
        }
    }

    @ApiOperation("test")
    @RequestMapping(value = "/importStudentInfo1", method = RequestMethod.POST)
    public void importStudentInfo1(@RequestBody MultipartFile file, HttpServletRequest request) throws Exception {
        ServletInputStream inputStream = request.getInputStream();
        System.err.println(inputStream);

        
//        int read = inputStream.read();
//        System.err.println(read);
        /*byte[] bytes = new byte[20480];
        int len=0;
         String s="";
        while ((len=inputStream.read(bytes))!=-1){
             s=new String(bytes,0,len,"UTF-8");
            System.err.println("换行");
        }*/
        System.err.println(inputStream);


        ImportParams params = new ImportParams();
        params.setHeadRows(1);//字段占几列
        params.setTitleRows(1);//标题几行

        //FileInputStream in = new FileInputStream("e:/studentInfo.xlsx");
       // ZipSecureFile.setMinInflateRatio(-1.0d);
        InputStream inputStream1 = file.getInputStream();

        List<Student> student = ExcelImportUtil.importExcel(inputStream1, Student.class, params);
        teacherService.importStudentInfo(student);
        for (Student student1 : student) {
            System.err.println(student1);
        }
       // inputStream.close();
       // in.close();
        System.err.println("ok");

    }

}
